﻿Namespace Security
    Friend Class FSelectSecurityGroups
        Private mResult As New Generic.List(Of Entities.SecurityGroup)
        Private mOptions As SelectGroupOptions

        Public Shadows Sub Show(ByVal owner As Form, ByVal options As SelectGroupOptions)
            InitView(options, False)
            MyBase.Show()
        End Sub

        Public Shadows Function ShowDialog(ByVal owner As Form, ByVal options As SelectGroupOptions) As Generic.List(Of Entities.SecurityGroup)
            InitView(options, True)
            MyBase.ShowDialog(owner)
            Return mResult
        End Function
        Private Sub InitView(ByVal options As SelectGroupOptions, ByVal runAsDialog As Boolean)
            mOptions = options
            Me.Text = options.Caption & " - Buddy 2 Buddy"
            For Each clsGroup As Entities.SecurityGroup In Handler.GetAll
                UpdateGroup(clsGroup)
            Next
            btnNew.Visible = options.NewAvailable
            lvwGroupMembership.MultiSelect = options.MultiSelect
            If Not runAsDialog Then
                btnOK.Visible = False
                Me.AcceptButton = btnCancel
                btnCancel.Text = "Close"
            End If
        End Sub

        Private Sub UpdateGroup(ByVal group As Entities.SecurityGroup)
            Dim strKey As String = group.Id.ToString
            If lvwGroupMembership.Items.ContainsKey(strKey) Then
                With lvwGroupMembership.Items(strKey)
                    .Text = group.Name
                    .SubItems(1).Text = group.Type.ToString
                    .SubItems(2).Text = group.Description
                    .Tag = group
                End With
            ElseIf Not mOptions.HideSecurityGroupIds.Contains(group.Id) Then
                With lvwGroupMembership.Items.Add(strKey, group.Name, CStr(Nothing))
                    .SubItems.Add(group.Type.ToString)
                    .SubItems.Add(group.Description)
                    .Tag = group
                End With
            End If
        End Sub

        Private Sub EventHandler_Ok(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles btnOK.Click, lvwGroupMembership.DoubleClick
            Try
                If btnOK.Visible Then

                    Dim aRet As New Generic.List(Of Entities.SecurityGroup)
                    For Each clsItem As ListViewItem In lvwGroupMembership.SelectedItems
                        aRet.Add(CType(clsItem.Tag, Entities.SecurityGroup))
                    Next
                    mResult = aRet
                    Me.Close()
                Else
                    EventHandler_EditSelected()
                End If
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Me.Close()
        End Sub

        Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
            Try
                Dim clsSG As New Entities.SecurityGroup
                clsSG.Type = Entities.SecurityGroupType.User
                clsSG.SubType = Entities.SecurityGroupSubType.None
                Dim clsOpts As New EditGroupOptions
                clsOpts.Caption = "Create new group"
                Manager.EditGroup(Me, clsSG, clsOpts)
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Sub FSelectSecurityGroups_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            RemoveHandler Framework.Security.Manager.UpdatedGUI, AddressOf EventHandler_SecurityGroupUpdated
        End Sub

        Private Sub FSelectSecurityGroups_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            AddHandler Framework.Security.Manager.UpdatedGUI, AddressOf EventHandler_SecurityGroupUpdated
        End Sub

        Private Sub EventHandler_SecurityGroupUpdated(ByVal group As Entities.SecurityGroup)
            Try
                UpdateGroup(group)
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
            EventHandler_EditSelected()
        End Sub
        Private Sub EventHandler_EditSelected()
            Try
                If lvwGroupMembership.SelectedItems.Count = 1 Then
                    Dim clsG As Entities.SecurityGroup = CType(lvwGroupMembership.SelectedItems(0).Tag, Entities.SecurityGroup)
                    Dim clsOpts As New EditGroupOptions
                    clsOpts.Caption = String.Format("Edit group {0}", clsG.Name)
                    clsOpts.EditMembersAvailable = True
                    Manager.EditGroup(Me, clsG, clsOpts)
                End If
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Sub lvwGroupMembership_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwGroupMembership.SelectedIndexChanged
            Try
                If lvwGroupMembership.SelectedItems.Count = 1 Then
                    btnEdit.Enabled = True
                    Select Case CType(lvwGroupMembership.SelectedItems(0).Tag, Entities.SecurityGroup).Type
                        Case Entities.SecurityGroupType.System
                            btnEdit.Text = "View..."
                        Case Else
                            btnEdit.Text = "Edit..."
                    End Select
                Else
                    btnEdit.Enabled = False
                End If

            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub
    End Class
End Namespace